package com.liaoban.app.ui.follow;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoban.app.R;
import com.liaoban.app.adapter.FollowAdapter;
import com.liaoban.app.api.type.Anchor;
import com.liaoban.app.api.type.Follow;
import com.liaoban.app.api.type.request.WuserinfoRequest;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.MainActivity;
import com.liaoban.app.ui.anchor.AnchorDetailActivity;
import com.liaoban.app.ui.base.BaseListFragment;
import com.liaoban.app.ui.login.LoginActivity;
import com.liaoban.app.ui.mine.FansActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ybc on 2018/7/10 0010.
 * 我的关注
 */
public class FollowFragment extends BaseListFragment<Follow> {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;

    @Override
    public BaseQuickAdapter getAdapter() {
        return new FollowAdapter(null);
    }

    @Override
    public void itemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        Follow follow = (Follow) baseQuickAdapter.getData().get(position);
        switch (view.getId()) {
            case R.id.iv_follow:
                showRunningDialog(R.string.committing);
                WuserinfoRequest wuserinfoRequest = new WuserinfoRequest();
                wuserinfoRequest.setWuserId(follow.getWuserId() + "");
                startTask(CommonBiz.getInstance().follow(wuserinfoRequest), baseResponse -> {
                    follow.setStatus(baseResponse.code);
                    ToastHelper.show(getActivity(), baseResponse.msg);
                    baseQuickAdapter.notifyItemChanged(position);
                });
                break;
        }
    }

    @Override
    public void itemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        if(!CommonBiz.getInstance().isLogin(getActivity())) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        Follow follow = (Follow) baseQuickAdapter.getData().get(position);
        startActivity(AnchorDetailActivity.newIntent(getActivity(), String.valueOf(follow.getWuserId())));
    }

    @Override
    public void getData() {
        startTask(CommonBiz.getInstance().myFollow(), listBaseResponse -> {
            doSuc(listBaseResponse.data, listBaseResponse.totalPageCount);
        });
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_follow;
    }

    @Override
    protected void initView(View contentView) {
        super.initView(contentView);
        toolbar.setNavigationOnClickListener(v -> {
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.changeTab(0);
        });//后退键
        tvTitle.setText(R.string.follow);
        tvRight.setText(R.string.fans);
        tvRight.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.tv_right)
    public void right() {
        startActivity(new Intent(getActivity(), FansActivity.class));
    }
}
